package cn.xiehang.SoundSight;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.List;

public class LikeOrStartAdapter extends RecyclerView.Adapter<LikeOrStartAdapter.LikeOrStarViewHolder> {
    private List<Article> articleList;
    private OnItemClickListener onItemClickListener;
    String State;
    String getImageUrl=SeverURL.URL+"/get_image/";

    public interface OnItemClickListener {
        void onItemClick(Article article);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public LikeOrStartAdapter(List<Article> articleList) {
        this.articleList = articleList;
    }
    public LikeOrStartAdapter(List<Article> articleList, String state) {
        this.articleList = articleList;
        this.State = state;
    }

    @NonNull
    @Override
    public LikeOrStarViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_like_or_star, parent, false);
        return new LikeOrStarViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull LikeOrStarViewHolder holder, int position) {
        Article article = articleList.get(position);
        holder.tvArticleTitle.setText(article.getTitle());
        holder.LikeOrStarTime.setText(" 文章创建时间：\n"+article.getCreateTime());

        //设置图片使用Glide
        Glide.with(holder.itemView.getContext())
                .load(getImageUrl + article.getImageUrl())
                .placeholder(R.drawable.ic_launcher_background)
                .into(holder.ivArticleImage);

        holder.lyLikeOrStar.setOnClickListener(v -> {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(article);
            }
        });
    }

    @Override
    public int getItemCount() {
        return articleList.size();
    }

    static class LikeOrStarViewHolder extends RecyclerView.ViewHolder {
        ImageView ivArticleImage;
        TextView tvArticleTitle,LikeOrStarTime;
        LinearLayout lyLikeOrStar;

        public LikeOrStarViewHolder(@NonNull View itemView) {
            super(itemView);
            ivArticleImage = itemView.findViewById(R.id.iv_like_or_star);
            tvArticleTitle = itemView.findViewById(R.id.tv_like_or_star_title);
            LikeOrStarTime = itemView.findViewById(R.id.tv_like_or_star_time);
            lyLikeOrStar = itemView.findViewById(R.id.fragment_like_or_star_layout);
        }
    }
}
